# ---------------------------------------------------------------------------
#This file uses ArcGIS 10.8.2 to construct the fire overlays and buffers for "Climate Risk and Preferences over the Size of Government."
#MC 11302022
# ---------------------------------------------------------------------------


import sys, time, logging, os, arcpy, shutil



#copy files to temp directory
shutil.copytree(r"raw_data", "temp")

#set up arcpy
arcpy.env.workspace = "temp"
arcpy.env.overwriteOutput = 1



fire_source=r"firesp21.shp"
block_source= r"tl_2010_06_bg10.shp"


try:
    
    #make the feature layers
    arcpy.MakeFeatureLayer_management(fire_source,"Fire_Selection")
    arcpy.MakeFeatureLayer_management(block_source,"Block_Targets")
    
  
  #loop through years  
    for years in range(2001, 2021, 2):
        
        yearname=str(years+1)
       
        joinname = "big" + str(years+1)
        
        #select the fires for each election
        if years == 2001:
            electiondate = "AND \"ALARM_DATE\" <=date '2002-11-05 00:00:00' AND \"ALARM_DATE\" >=date '2000-11-07 00:00:00'"
        elif years == 2003:
            electiondate = "AND \"ALARM_DATE\" <=date '2004-11-02 00:00:00' AND \"ALARM_DATE\" >=date '2002-11-06 00:00:00'"
        elif years == 2005:
            electiondate = "AND \"ALARM_DATE\" <=date '2006-11-07 00:00:00' AND \"ALARM_DATE\" >=date '2004-11-03 00:00:00'"
        elif years == 2007:
            electiondate = "AND \"ALARM_DATE\" <=date '2008-11-04 00:00:00'AND \"ALARM_DATE\" >=date '2006-11-08 00:00:00'"
        elif years == 2009:
            electiondate = "AND \"ALARM_DATE\" <=date '2010-11-02 00:00:00'AND \"ALARM_DATE\" >=date '2008-11-05 00:00:00'"
        elif years == 2011:
            electiondate = "AND \"ALARM_DATE\" <=date '2012-11-06 00:00:00' AND \"ALARM_DATE\" >=date '2010-11-03 00:00:00'"
        elif years == 2013:
            electiondate = "AND \"ALARM_DATE\" <=date '2014-11-04 00:00:00' AND \"ALARM_DATE\" >=date '2012-11-07 00:00:00'"
        elif years == 2015:
            electiondate = "AND \"ALARM_DATE\" <=date '2016-11-08 00:00:00'AND \"ALARM_DATE\" >=date '2014-11-05 00:00:00'"
        elif years == 2017:
            electiondate = "AND \"ALARM_DATE\" <=date '2018-11-06 00:00:00'AND \"ALARM_DATE\" >=date '2016-11-09 00:00:00'"
        else:
            electiondate = "AND \"ALARM_DATE\" <=date '2020-12-31 00:00:00'AND \"ALARM_DATE\" >=date '2018-12-31 00:00:00'"            
            
        sql="\"GIS_ACRES\" >=5000" + electiondate
        arcpy.SelectLayerByAttribute_management("Fire_Selection", "NEW_SELECTION", sql)
     
        #spatial join fire perimeters to block groups
        arcpy.analysis.SpatialJoin(r"Block_Targets", r"Fire_Selection", joinname)
        print(joinname)
      
    #loop through buffers   
    bufferdistances=["5", "10", "20"]  
    for num in bufferdistances:
    
    #loop through years
        for years in range(2001, 2021, 2):
            joinname = "buffjoin" + str(num) + "k" + str(years+1)
            buffname = "buff" + str(num) + "k" + str(years+1)
            print(joinname)
            if years == 2001:
                electiondate = "AND \"ALARM_DATE\" <=date '2002-11-05 00:00:00' AND \"ALARM_DATE\" >=date '2000-11-07 00:00:00'"
            elif years == 2003:
                electiondate = "AND \"ALARM_DATE\" <=date '2004-11-02 00:00:00' AND \"ALARM_DATE\" >=date '2002-11-06 00:00:00'"
            elif years == 2005:
                electiondate = "AND \"ALARM_DATE\" <=date '2006-11-07 00:00:00' AND \"ALARM_DATE\" >=date '2004-11-03 00:00:00'"
            elif years == 2007:
                electiondate = "AND \"ALARM_DATE\" <=date '2008-11-04 00:00:00'AND \"ALARM_DATE\" >=date '2006-11-08 00:00:00'"
            elif years == 2009:
                electiondate = "AND \"ALARM_DATE\" <=date '2010-11-02 00:00:00'AND \"ALARM_DATE\" >=date '2008-11-05 00:00:00'"
            elif years == 2011:
                electiondate = "AND \"ALARM_DATE\" <=date '2012-11-06 00:00:00' AND \"ALARM_DATE\" >=date '2010-11-03 00:00:00'"
            elif years == 2013:
                electiondate = "AND \"ALARM_DATE\" <=date '2014-11-04 00:00:00' AND \"ALARM_DATE\" >=date '2012-11-07 00:00:00'"
            elif years == 2015:
                electiondate = "AND \"ALARM_DATE\" <=date '2016-11-08 00:00:00'AND \"ALARM_DATE\" >=date '2014-11-05 00:00:00'"
            elif years == 2017:
                electiondate = "AND \"ALARM_DATE\" <=date '2018-11-06 00:00:00'AND \"ALARM_DATE\" >=date '2016-11-09 00:00:00'"
            else:
                electiondate = "AND \"ALARM_DATE\" <=date '2020-12-31 00:00:00'AND \"ALARM_DATE\" >=date '2018-12-31 00:00:00'"             
                    
            sql="\"GIS_ACRES\" >=5000" + electiondate
            arcpy.SelectLayerByAttribute_management("Fire_Selection", "NEW_SELECTION", sql)
                
            #make buffers then calculate spatial join with block groups
            arcpy.Buffer_analysis("Fire_Selection", buffname+".shp", str(num) +" Kilometers", "FULL", "ROUND","ALL")
            arcpy.MakeFeatureLayer_management(buffname+".shp","Buffers")
            arcpy.analysis.SpatialJoin(r"Block_Targets", "Buffers", joinname)           

except Exception as err:
    print(err.args[0])
    sys.exit("Exception")

print("Done")   



